import socket
import network
from mpython import *

mywifi=wifi()                    # 实例化wifi类
mywifi.connectWiFi("  "," ")           
time.sleep(5)
oled.fill(0)
oled.DispChar(str(mywifi.sta.ifconfig()[0]), 0, 0)
oled.show()
time.sleep(2)

CONTENT=b'''\
HTTP/1.1 200 OK

OK!
'''

addr = socket.getaddrinfo(mywifi.sta.ifconfig()[0], 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
oled.fill(0)
oled.DispChar('Connect on', 0, 0)
oled.DispChar(str(addr), 0, 16)
oled.show()

while True:
  conn, addr = s.accept()
  #print('Connection from %s' %str(addr))
  request = conn.recv(4096).decode('utf-8')
  request=request.split('\r\n')
  pic_str=request[-2]
  pic_str.strip('(')#删除某个字符
  pic_str.strip(')')
  #print('Content = %s' %pic_str)
  #print(pic_str)
  pic_list=pic_str.split(',')
  #print(str(pic_list))
  pic_list_show=[]
  for i in range(0,len(pic_list)):
      pic_list_show.append(int(pic_list[i]))
  #print(str(pic_list_show))
  oled.fill(0)
  oled.Bitmap(0, 0, pic_list_show, 128, 64, 1)
  oled.show()
  del pic_list_show
  conn.send(CONTENT)#在接收到客户端传递过来的信息时，一定要回复客户端，这是必须的，并且是以二进制报文的形式回复，否则APP端会报Error1103的错误
  conn.close()
